/*     */ package com.xhgk.utis;
/*     */ 
/*     */ import com.xhgk.domain.ConstantParam;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.StringReader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import org.apache.lucene.analysis.Analyzer;
/*     */ import org.apache.lucene.analysis.TokenStream;
/*     */ import org.apache.lucene.analysis.tokenattributes.TermAttribute;
/*     */ import org.apache.lucene.index.CorruptIndexException;
/*     */ import org.apache.lucene.index.IndexReader;
/*     */ import org.apache.lucene.index.IndexWriter;
/*     */ import org.apache.lucene.index.IndexWriterConfig;
/*     */ import org.apache.lucene.search.IndexSearcher;
/*     */ import org.apache.lucene.store.Directory;
/*     */ import org.apache.lucene.store.FSDirectory;
/*     */ import org.apache.lucene.store.LockObtainFailedException;
/*     */ import org.apache.lucene.util.Version;
/*     */ import org.wltea.analyzer.lucene.IKAnalyzer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LuceneTool
/*     */ {
/*     */   private static IndexWriter indexWriter;
/*     */   private static IndexSearcher searcher;
/*     */   private static Directory directory;
/*  40 */   private static Analyzer analyzer = (Analyzer)new IKAnalyzer(); static {
/*     */     try {
/*  42 */       directory = (Directory)FSDirectory.open(new File(ConstantParam.indexDirectory));
/*  43 */     } catch (IOException e1) {
/*  44 */       e1.printStackTrace();
/*     */     } 
/*     */ 
/*     */     
/*  48 */     Runtime.getRuntime().addShutdownHook(new Thread()
/*     */         {
/*     */           public void run() {
/*  51 */             LuceneTool.closeIndexWriter();
/*  52 */             LuceneTool.closeIndexSearcher();
/*     */           }
/*     */         });
/*     */   }
/*     */   
/*     */   public static IndexWriter getIndexWriter() {
/*  58 */     if (searcher == null) {
/*  59 */       IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
/*     */       try {
/*  61 */         indexWriter = new IndexWriter(directory, config);
/*  62 */       } catch (CorruptIndexException e) {
/*     */         
/*  64 */         e.printStackTrace();
/*  65 */       } catch (LockObtainFailedException e) {
/*     */         
/*  67 */         e.printStackTrace();
/*  68 */       } catch (IOException e) {
/*     */         
/*  70 */         e.printStackTrace();
/*     */       } 
/*     */     } 
/*  73 */     return indexWriter;
/*     */   }
/*     */   
/*     */   public static IndexSearcher getSearcher() {
/*  77 */     if (searcher == null) {
/*     */       try {
/*  79 */         searcher = new IndexSearcher(IndexReader.open(directory));
/*  80 */       } catch (Exception e) {
/*  81 */         throw new RuntimeException(e);
/*     */       } 
/*     */     }
/*  84 */     return searcher;
/*     */   }
/*     */ 
/*     */   
/*     */   public static void closeIndexWriter() {
/*  89 */     if (indexWriter != null) {
/*     */       try {
/*  91 */         indexWriter.close();
/*  92 */       } catch (Exception e) {
/*  93 */         throw new RuntimeException(e);
/*     */       } 
/*     */     }
/*     */   }
/*     */ 
/*     */   
/*     */   public static void closeIndexSearcher() {
/* 100 */     if (searcher != null) {
/*     */       try {
/* 102 */         searcher.close();
/* 103 */       } catch (Exception e) {
/* 104 */         throw new RuntimeException(e);
/*     */       } 
/*     */     }
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public static List<String> getParticiple(String searchValue) {
/* 112 */     List<String> valList = new ArrayList<String>();
/* 113 */     TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(searchValue));
/* 114 */     tokenStream.addAttribute(TermAttribute.class);
/*     */     try {
/* 116 */       while (tokenStream.incrementToken()) {
/* 117 */         TermAttribute termAttribute = (TermAttribute)tokenStream
/* 118 */           .getAttribute(TermAttribute.class);
/* 119 */         String msg = termAttribute.term().replaceAll(" ", "").trim();
/* 120 */         if (!"".equals(msg) && !valList.contains(msg)) {
/* 121 */           valList.add(msg);
/*     */         }
/*     */       } 
/* 124 */     } catch (IOException e1) {
/* 125 */       e1.printStackTrace();
/*     */     } 
/* 127 */     return valList;
/*     */   }
/*     */   
/*     */   public static Analyzer getAnalyzer() {
/* 131 */     return analyzer;
/*     */   }
/*     */   
/*     */   public static void setAnalyzer(Analyzer analyzer) {
/* 135 */     LuceneTool.analyzer = analyzer;
/*     */   }
/*     */ }


/* Location:              D:\library\tomcat\webapps\ROOT\WEB-INF\classes\!\com\xhg\\utis\LuceneTool.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       1.1.3
 */